package com.kuang.blog.shiro;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    //总结：

    /**
     * 1.建立realm(规则)
     * @return
     */
    @Bean("myRealm")
    public UserRealm myRealm(){
        return new UserRealm();
    }
    /**
     * 2.securityManager 流程控制 将realm设置进DefaultWebSecurityManager
     *
     */
    @Bean("mySecurityManager")
    public DefaultWebSecurityManager securityManager(@Qualifier("myRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }
    /**
     * 3.ShiroFilterFactoryBean请求过滤器
     * 将securityManager设置进ShiroFilterFactoryBean
     */
   @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(@Qualifier("mySecurityManager") SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        //过滤域
        Map<String, Filter> filters = new HashMap<>();
        filters.put("auth", new JwtFilter());
        shiroFilter.setFilters(filters);
        //过滤链
        Map<String,String> map =  new LinkedHashMap<>();
        map.put("/login","anon");//设置为公共资源，不放行会进入死循环  放行资源放在下面
        map.put("/blog/retrieveAll","anon");//设置为公共资源，不放行会进入死循环  放行资源放在下面
        map.put("/**","auth");
        //配置认证和授权规则
        shiroFilter.setFilterChainDefinitionMap(map);

        return shiroFilter;
    }
    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    /**
     * @description:开启代码权限注解支持
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    /**
     * @description:解决权限注解不生效问题
     */
    @Bean
    public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator(){
        return new DefaultAdvisorAutoProxyCreator();
    }
    @Bean
    public FilterRegistrationBean delegatingFilterProxy(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        DelegatingFilterProxy proxy = new DelegatingFilterProxy();
        proxy.setTargetFilterLifecycle(true);
        proxy.setTargetBeanName("shiroFilter");
        filterRegistrationBean.setFilter(proxy);
        return filterRegistrationBean;
    }
}
